package com.dengyin2000.javaeye.ordering.entity;

import java.io.Serializable;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import org.hibernate.validator.Email;
import org.hibernate.validator.Length;
import org.hibernate.validator.NotNull;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Role;
import org.jboss.seam.annotations.Roles;

@Entity
@Name("user")
@Table(name="user")
@Roles({@Role(name="currentUser", scope=ScopeType.SESSION)})
public class User implements Serializable {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 5612268487735887460L;
	//seam-gen attributes (you should probably edit these)
	private Long id;
	private String loginName;
	private String realName;
	private String password;
	private String email;
	private Date registeDate;
	
	private Set<com.dengyin2000.javaeye.ordering.entity.Role> roles = new HashSet<com.dengyin2000.javaeye.ordering.entity.Role>();
	
	private Set<DishOrder> orders = new HashSet<DishOrder>();

	@Column(name="registe_date")
	public Date getRegisteDate() {
		return registeDate;
	}

	public void setRegisteDate(Date registeDate) {
		this.registeDate = registeDate;
	}

	@Id @GeneratedValue(strategy=GenerationType.IDENTITY)
	public Long getId() {
	     return id;
	}

	public void setId(Long id) {
	     this.id = id;
	}
	
	@NotNull
	@Length(max=20)
	@Column(name="real_name")
	public String getRealName() {
	     return realName;
	}

	public void setRealName(String name) {
	     this.realName = name;
	}

	@NotNull
	@Length(max=20)
	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}
	@Column(unique=true, name="login_name")
	@NotNull
	public String getLoginName() {
		return loginName;
	}

	public void setLoginName(String loginName) {
		this.loginName = loginName;
	}

	@Column(unique=true)
	@NotNull
	@Email
	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}
	
	@ManyToMany(cascade={CascadeType.PERSIST, CascadeType.MERGE})
	@JoinTable(name="user_role", joinColumns= {@JoinColumn(name="user_id")}, inverseJoinColumns= {@JoinColumn(name="role_id")})
	public Set<com.dengyin2000.javaeye.ordering.entity.Role> getRoles() {
		return roles;
	}

	public void setRoles(Set<com.dengyin2000.javaeye.ordering.entity.Role> roles) {
		this.roles = roles;
	}   
	
	public void addRole(com.dengyin2000.javaeye.ordering.entity.Role role) {
		roles.add(role);
	}

	@OneToMany(mappedBy="user", cascade=CascadeType.ALL)
	public Set<DishOrder> getOrders() {
		return orders;
	}

	public void setOrders(Set<DishOrder> orders) {
		this.orders = orders;
	}
}
